package org.codert.jpa.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.Data;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;

/**
 * @author: TanJingyu
 * @create: 2021-09-21 12:05
 **/
//@Api(tags = "参数校验测试模块")
//@RestController
//@RequestMapping("/validate")
public class ValidatedController {

//    @ApiImplicitParam(name = "name",value = "姓名",required = true)
    @ApiOperationSupport(author = "谭景玉")
    @ApiOperation(value = "校验测试方法，post的")
    @PostMapping
    public ValidateResponse validate(@Validated @ApiParam(name = "out", value = "实体", required = true, example = "{\"name\":\"谭景玉\",\"age\":20,\"inner\":{\"size\":10}}")
                               @RequestBody ValidateDemoOut out) {
        return new ValidateResponse();
    }

    @Data
    public static class ValidateResponse {
        @ApiModelProperty(name = "inner", required = true, value = "嵌套实体")
        private String code;
        @ApiModelProperty(name = "inner", required = true, value = "嵌套实体")
        private String message;
    }

    @Data
    public static class ValidateDemoOut {
        private String name;
        private Integer age;
        @Valid
        @NotNull
        @ApiModelProperty(name = "inner", required = true, value = "嵌套实体")
        private ValidateDemoInner inner;

        public static void main(String[] args) throws JsonProcessingException {
            ValidateDemoOut out = new ValidateDemoOut();
            out.setAge(20);
            out.setName("谭景玉");
            ValidateDemoInner inner = new ValidateDemoInner();
            inner.setSize(10);
            out.setInner(inner);

            ObjectMapper objectMapper = new ObjectMapper();
            System.out.println(objectMapper.writeValueAsString(out));
        }
    }

    @Data
    public static class ValidateDemoInner {
        @Max(value = 5, message = "超过大小限制")
//        @NotNull
        @ApiModelProperty(name = "size", required = true, value = "大小，最大不能超过5")
        private Integer size;
    }
}
